package com.example.demo.controller;

import com.example.demo.model.Person;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author Hsu琛君珩
 * @ClassName:ParamController
 * @date 2023-11-26
 * @apiNote
 * @Version: v1.0
 */

@RequestMapping("/param")
@RestController
public class ParamController {
    @RequestMapping("/m1")
    public String m1(String name){
        return "接收到的参数name:"+name;
    }

    @RequestMapping("/m2")
    public String m2(String name,Integer age){
        return"接收到的参数name:"+name+"，age:"+age;
    }

    @RequestMapping("/m3")
    public String m3(String name,int age){
        return"接收到的参数name:"+name+"，age:"+age;
    }

    @RequestMapping("/m4")
    public String m4(Person person){
        return "接收到的参数person:"+person.toString();
    }

    @RequestMapping("/m5")
    public String m5(@RequestParam(value = "name",required = false) String username){
        return "接收到的参数name:"+username;
    }

    @RequestMapping("/m6")
    public String m6(String[] arrayParam){
        return "接收到的参数arrayParam:"+ Arrays.toString(arrayParam)+"，长度:"+arrayParam.length;
    }

    @RequestMapping("/m7")
    public String m7(@RequestParam List<String> listParam){
        return "接收到的参数listParam:"+ listParam+"，长度:"+listParam.size();
    }

    @RequestMapping("/m8")
    public String m8(@RequestBody Person person){
        return "接收到的参数person:"+ person.toString();
    }

    @RequestMapping("/m9/{userId}/{name}")
    public String m9(@PathVariable Integer userId,@PathVariable String name){
        return "接收到的参数userId:"+ userId+",name:"+name;
    }

    @RequestMapping("/m10")
    public String m10(@RequestPart MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:/tempidea/"+file.getOriginalFilename()));
        return "success";
    }

    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request, HttpServletResponse response){
        //这两个参数是Spring的内置对象，需要使用时，直接在方法声明即可
        Cookie[] cookies = request.getCookies();
        //for (Cookie cookie : cookies) {
        //    System.out.println(cookie.getName()+":"+cookie.getValue());
        //}
        if(cookies!=null) {
            Arrays.stream(cookies).forEach(cookie -> {
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            });
        }
        return "获取cookie成功";
    }

    @RequestMapping("/getCookie2")
    public String getCookie(@CookieValue String bite){
        return "cookie中存储的值bite:"+bite;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.setAttribute("username","zhangsan");
        return "success";
    }
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session=request.getSession(false);//true的话不存在session就创建一个，false不存在就不创建
                                                        //默认值为true
        //根据sessionId获取session对象，类似根据学号找到整个学生
        if(session!=null){
            String username= (String) session.getAttribute("username");
            return "登录用户:"+username;
        }
        return "session 为空";
    }

    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute(required = false) String username){
        return "username:"+username;
    }

    @RequestMapping("/getSession3")
    public String getSession3(HttpSession session){
        String username= (String) session.getAttribute("username");
        return "登录用户:"+username;
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "userAgent:"+userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent")String userAgent){
        return "userAgent:"+userAgent;
    }

    @RequestMapping("/m11")
    public String m11(){
        return "";
    }
}
